<template>
  <el-dialog
    title="项目总成本计划列表"
    :visible.sync="show"
    width="900px"
    center
    append-to-body
  >
    <div class="content-box">
      <com-table
        v-model="selectNode"
        highlight-current-row
        :data="dataList"
        :columns="columns"
        :total="total"
        @pageChange="pageChange"
      >
        <template v-slot:createTime="{row: {createTime}}">
          {{ dateFormat(createTime) }}
        </template>
        <template v-slot:projStatus="{row: {projStatus}}">
          {{ getDictName(projectInfoProjStatus, projStatus) }}
        </template>
        <template v-slot:projType="{row: {projType}}">
          {{ getDictName(projectInfoProjType, projType) }}
        </template>
        <template v-slot:projMode="{row: {projMode}}">
          {{ getDictName(projectInfoProjMode, projMode) }}
        </template>
        <template v-slot:projectDate="{row: {projectDate}}">
          {{ dateFormat(projectDate) }}
        </template>
        <template v-slot:planStartTime="{row: {planStartTime}}">
          {{ dateFormat(planStartTime) }}
        </template>
        <template v-slot:planFinishTime="{row: {planFinishTime}}">
          {{ dateFormat(planFinishTime) }}
        </template>
        <template v-slot:realStartTime="{row: {realStartTime}}">
          {{ dateFormat(realStartTime) }}
        </template>
        <template v-slot:options="{row}">
          <el-button type="success" size="mini" @click="getCheckList(row)">引入</el-button>
        </template>
      </com-table>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getProjectTotalCostPlanFinishInfoList, getProjectTotalCostPlanDetailList } from '@/api/contractManage'
import moment from 'moment'
import { getDictName } from '@/utils/comm'
import { projectInfoProjStatus, projectInfoProjType, projectInfoProjMode } from '@/utils/dicData'
export default {
  name: 'ProjectList',
  components: {

  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    selectSearch: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      projectInfoProjStatus,
      projectInfoProjType,
      projectInfoProjMode,
      getDictName,
      visible: false,
      name: '',
      total: 0,
      pageParams: {
        pageNum: 1,
        pageSize: 10
      },
      selectNode: null,
      searching: false,
      dataList: [], // 列表
      columns: [
        {
          label: '项目名称',
          prop: 'projectName'
        },
        {
          label: '合同金额',
          prop: 'contractAmount'
        },
        {
          label: '项目计划成本',
          prop: 'planCost'
        },
        {
          label: '上交金额',
          prop: 'handedAmount'
        },
        {
          label: '上交率',
          prop: 'handedRat'
        }
      ]
    }
  },
  computed: {
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    showModal: {
      handler(newData) {
        if (newData) {
          this.getDataList()
        }
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {

  },
  methods: {
    dateFormat(data) {
      if (data) {
        return moment(data).format('YYYY-MM-DD')
      } else {
        return ''
      }
    },
    confirm() {
      if (this.selectNode) {
        this.getCheckList(this.selectNode)
      } else {
        this.show = false
      }
    },
    // 根据id获取 项目清单列表
    getCheckList(obj) {
      this.searching = true
      if (this.selectSearch) {
        getProjectTotalCostPlanDetailList({ projectTotalCostPlanInfoId: obj.id }).then(res => {
          this.searching = false
          const { data } = res || {}
          if (data) {
            // this.dealData()
            this.targetList = data
            // this.confirm({ row: obj, list: data })
            this.$emit('getSelectedRows', { row: obj, list: data })
            this.show = false
          }
        }).catch(() => { this.searching = false })
      } else {
        this.$emit('getSelectedRows', { row: obj })
        this.show = false
      }
    },
    dealData(arr) {
      const tmpArr = []
      const obj = {
        'constSumPrice': '',
        'amount': '',
        'contractSumPrice': '',
        'price': '',
        'costUnitPrice': '',
        'createTime': '',
        'description': '',
        'handedAmount': '',
        // 'id': 0,
        'inputTax': '',
        'name': '',
        'number': '',
        'parentId': '',
        'projectTotalCostPlanInfoId': '',
        'unit': '',
        'dddd': ''
      }
      arr.map(item => {
        obj['contractSumPrice'] = item.price
        obj['contractSumPrice'] = item.amount
        obj['name'] = item.name
        obj['number'] = item.number
        obj['unit'] = item.unit
        tmpArr.push(obj)
      })
      // 数据对应
      // const keyObj = {
      //   amount: 'contractSumPrice', // 金额
      //   contractWorkVolume: '', // 合同工程量
      //   createTime: '', // 编制时间
      //   drawingWorkVolume: '', // 图纸工程量
      //   enable: '', // 是否启用
      //   level: '', // 层级码字段
      //   measuredWorkVolume: '', // 实测工程量
      //   name: 'name', // 清单名称
      //   number: 'number', // 清单编号
      //   price: 'price', // 单价
      //   remark: '', // 备注
      //   unit: 'unit' // 清单单位
      // }
      return tmpArr
    },
    // confirm(obj) {
    //   this.show = false
    //   this.$emit('getSelectedRows', obj)
    // },
    pageChange(data) {
      this.pageParams = data
      this.getDataList()
    },
    getSelectedRows(data) {

    },
    getDataList() {
      const params = {
        ...this.pageParams
      }
      getProjectTotalCostPlanFinishInfoList(params).then(res => {
        const { data, total } = res.data || {}
        if (data) {
          this.dataList = data
          this.total = total - 0
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
::v-deep .content-box{
  overflow: hidden;
}
</style>
